package com.sunday.scoremanager.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.ClearEditText;
import com.sunday.scoremanager.R;
import com.sunday.scoremanager.base.BaseApp;
import com.sunday.scoremanager.entity.Member;
import com.sunday.scoremanager.http.ApiClient;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/11/9.
 */

public class RegisterFinishActivity extends BaseActivity {


    @Bind(R.id.left_btn)
    ImageView leftBtn;
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.right_btn)
    ImageView rightBtn;
    @Bind(R.id.common_header)
    RelativeLayout commonHeader;
    @Bind(R.id.edit_pwd)
    ClearEditText editPwd;
    @Bind(R.id.edit_confirm_pwd)
    ClearEditText editConfirmPwd;
    @Bind(R.id.btn_register)
    TextView btnRegister;


    private String mobile, code;
    private int type;//0:注册 1：修改密码 2：忘记密码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_finish);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mobile = getIntent().getStringExtra("mobile");
        code = getIntent().getStringExtra("code");
        type = getIntent().getIntExtra("type", 0);
        switch (type) {
            case 0:
                titleView.setText("注册");
                break;
            case 1:
                titleView.setText("修改密码");
                btnRegister.setText("确认");
                break;
            case 2:
                titleView.setText("忘记密码");
                btnRegister.setText("确认");
                break;
        }
    }

    private boolean isAgree = false;
    private String pwd, pwd1;

    @OnClick({R.id.btn_register})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_register:
                pwd = editPwd.getText().toString().trim();
                pwd1 = editConfirmPwd.getText().toString().trim();
                if (TextUtils.isEmpty(pwd) || TextUtils.isEmpty(pwd1)) {
                    ToastUtils.showToast(mContext, "请输入密码");
                    return;
                }
                if (!pwd.equals(pwd1)) {
                    ToastUtils.showToast(mContext, "两次密码输入不一致");
                    return;
                }
                switch (type) {
                    case 1:
                        titleView.setText("修改密码");
                        updatePwd();
                        break;
                    case 2:
                        titleView.setText("忘记密码");
                        updatePwd();
                        break;
                }

                break;
        }
    }


    private void updatePwd() {
        Call<ResultDO> call = ApiClient.getApiAdapter().findPwd(mobile, code, pwd);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                ResultDO<Member> resultDO = response.body();
                if (response.body().getCode() == 0) {
                    ToastUtils.showToast(mContext, "设置成功");
                    switch (type) {
                        case 1:
                            finish();
                            break;
                        case 2:
                            finish();
                            break;
                    }
                } else {
                    ToastUtils.showToast(mContext, resultDO.getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }
}

